package com.yuexinduo.education.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.yuexinduo.baselibrary.base.BaseMvpActivity;
import com.yuexinduo.education.R;
import com.yuexinduo.education.fragment.BranchFragment;
import com.yuexinduo.education.fragment.HomeFragment;
import com.yuexinduo.education.fragment.MineFragment;
import com.yuexinduo.education.fragment.MsgFragment;

@Route(path = "com.yuexinduo.education.ui.MainActivity")
public class MainActivity extends BaseMvpActivity implements View.OnClickListener {
    private FrameLayout educationFrame;
    private LinearLayout idTab1;
    private ImageButton idTabImg1;
    private LinearLayout idTab2;
    private ImageButton idTabImg2;
    private LinearLayout idTab3;
    private ImageButton idTabImg3;
    private LinearLayout idTab4;
    private ImageButton idTabImg4;
    private HomeFragment homeFragment;
    private BranchFragment branchFragment;
    private MsgFragment msgFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.red)
                .init();
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected void initCreate() {
        startActivity(new Intent(getActivity(), LaunchActivity.class));



        educationFrame = (FrameLayout) findViewById(R.id.education_frame);
        idTab1 = (LinearLayout) findViewById(R.id.id_tab1);
        idTabImg1 = (ImageButton) findViewById(R.id.id_tab_img1);
        idTab2 = (LinearLayout) findViewById(R.id.id_tab2);
        idTabImg2 = (ImageButton) findViewById(R.id.id_tab_img2);
        idTab3 = (LinearLayout) findViewById(R.id.id_tab3);
        idTabImg3 = (ImageButton) findViewById(R.id.id_tab_img3);
        idTab4 = (LinearLayout) findViewById(R.id.id_tab4);
        idTabImg4 = (ImageButton) findViewById(R.id.id_tab_img4);

        idTab1.setOnClickListener(this);
        idTab2.setOnClickListener(this);
        idTab3.setOnClickListener(this);
        idTab4.setOnClickListener(this);
        //初始化控件
        selectTab(0);//默认选中第一个Tab
        resetImg(0);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.id_tab1:
                resetImg(0);
                selectTab(0);
                break;
            case R.id.id_tab2:
                resetImg(1);
                selectTab(1);
                break;
            case R.id.id_tab3:
//                if (!LoginUtil.isLogin(this)){
//                    startActivity(new Intent(this,JobLoginActivity.class));
//                    return;
//                }
                resetImg(2);
                selectTab(2);
                break;
            case R.id.id_tab4:
//                if (!LoginUtil.isLogin(JobMainActivity.this)){
//                    startActivity(new Intent(JobMainActivity.this,JobLoginActivity.class));
//                    return;
//                }
                resetImg(3);
                selectTab(3);
                break;
        }
    }
    private void selectTab(int i) {
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            //当选中点击的是第一页的Tab时
            case 0:
                //如果第一页对应的Fragment没有实例化，则进行实例化，并显示出来
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.education_frame, homeFragment);
                } else {
                    //如果第一页对应的Fragment已经实例化，则直接显示出来
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                if (branchFragment == null) {
                    branchFragment = new BranchFragment();
                    transaction.add(R.id.education_frame, branchFragment);
                } else {
                    transaction.show(branchFragment);
                }
                break;
            case 2:
                if (msgFragment == null) {
                    msgFragment = new MsgFragment();
                    transaction.add(R.id.education_frame, msgFragment);
                } else {
                    transaction.show(msgFragment);
                }
                break;
            case 3:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.education_frame, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }
                break;
        }
        //不要忘记提交事务
        transaction.commit();
    }


    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (branchFragment != null) {
            transaction.hide(branchFragment);
        }
        if (msgFragment != null) {
            transaction.hide(msgFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
    }
    //将四个ImageButton置为灰色
    private void resetImg(int num) {

        switch (num){
            case 0:

                idTabImg1.setImageResource(R.mipmap.education_home_two);
                idTabImg2.setImageResource(R.mipmap.education_branch_one);
                idTabImg3.setImageResource(R.mipmap.education_msg_one);
                idTabImg4.setImageResource(R.mipmap.education_mine_one);

                break;
            case 1:
                idTabImg1.setImageResource(R.mipmap.education_home_one);
                idTabImg2.setImageResource(R.mipmap.education_branch_two);
                idTabImg3.setImageResource(R.mipmap.education_msg_one);
                idTabImg4.setImageResource(R.mipmap.education_mine_one);

                break;
            case 2:
                idTabImg1.setImageResource(R.mipmap.education_home_one);
                idTabImg2.setImageResource(R.mipmap.education_branch_one);
                idTabImg3.setImageResource(R.mipmap.education_msg_two);
                idTabImg4.setImageResource(R.mipmap.education_mine_one);
                break;

            case 3:

                idTabImg1.setImageResource(R.mipmap.education_home_one);
                idTabImg2.setImageResource(R.mipmap.education_branch_one);
                idTabImg3.setImageResource(R.mipmap.education_msg_one);
                idTabImg4.setImageResource(R.mipmap.education_mine_two);

                break;
        }


    }
}